package org.sgx.gwteditors.client.impl1.simple;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.EditorValueChangeEvent;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.impl1.basetypes.Color;
import org.sgx.gwteditors.client.impl1.ui.colorpicker2.ColorPicker;
import org.sgx.gwteditors.client.util.GUIUtil;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * color editor model type for org.sgx.gwteditors.client.impl1.basetypes.Color
 * Visually is a textbox showing the color in the background, that will launh a color picker dialog
 * on click and also can be edited with the keyboard.
 *  
 * @author sg
 *
 */
public class ColorEditor extends TextBox implements Editor<Color>, ClickHandler, ValueChangeHandler<String> {

	private ColorPicker colorPicker = null;
	private Color model;
	private DialogBox colorPickerDB;
//	private boolean isNullValue=false;
	
	public ColorEditor() {
		super();
//		setWidth("80px");
		addClickHandler(this);
		addValueChangeHandler(this);
		colorPicker = getColorPicker();
//		addKeyPressHandler(this);
	}
	@Override
	public void addValueChangeListener(ValueChangeListener<Color> l) {
		EditorEventManager.<Color>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<Color> l) {
		EditorEventManager.<Color>getInstance().removeValueChangeListener(this, l);
	}
	@Override
	public List<EditorError<Color>> getErrors() {
		return EditorErrorManager.<Color>getInstance().getErrors(this);
	}
	@Override
	public Color flush() {		
		model.setHex(colorPicker.getHexColor());
		return model;//no validation / errors
	}

	@Override
	public void load(Color model_) {
//		System.out.println("ColorEditor loading model: "+model_);
		if(model_==null) {
//			this.isNullValue=true;
			this.model=new Color("ffffff");
			model.setNone(true);
		}
		else {
			this.model=model_;
		}
		String hex = model.getHex();
		doSetHex(hex);
	}

	@Override
	public Widget getWidget() {
		return this;
	}

	@Override
	public boolean isSimple() {
		return true;
	}
	
	@Override
	public void onClick(ClickEvent event) {
		// label was clicked, show the colorshooser dialog
//		colorPicker = getColorPicker();
		GUIUtil.showDialogBox(colorPickerDB);
//		Window.alert("clicked");
	}
	
	
	private ColorPicker getColorPicker() {
		if(colorPicker==null) {
			VerticalPanel vp = new VerticalPanel();
			colorPicker =new ColorPicker();
			
			vp.add(colorPicker);
			FlowPanel fp1 = new FlowPanel();
			fp1.add(new Button("Accept", new ClickHandler() {				
				@Override
				public void onClick(ClickEvent event) {
					doSetHex(colorPicker.getHexColor());
					EditorEventManager.<Color>getInstance().fireValueChangeEvent(ColorEditor.this);		
					colorPickerDB.hide();
					ColorEditor.this.setFocus(true);
				}
			}));
			fp1.add(new Button("Cancel", new ClickHandler() {				
				@Override
				public void onClick(ClickEvent event) {
					colorPickerDB.hide();
					ColorEditor.this.setFocus(true);
				}
			}));
			vp.add(fp1);
			
			colorPickerDB = GUIUtil.createDialogBox("Choose color...", vp);
		}
		return colorPicker;
	}
	@Override
	public void onValueChange(ValueChangeEvent<String> event) { //value change on this textBox gwt widget
		doSetHex(getText());
		//finnaly notify listeners:
//		EditorValueChangeEvent<Color> vcEvent = new EditorValueChangeEvent<Color>(this, model);
		EditorEventManager.<Color>getInstance().fireValueChangeEvent(this);		
	}
	private void doSetHex(String text) {
		if(text==null)
			return;
//		if(text.equals("none")) {
//			model.setNone(true);
//		}
		
//		System.out.println("SETHEXTOMODEL: "+text+" objid: "+this.hashCode());
		model.setHex(text);
//		model.setNone(false);
				
		
		if(model.isNone()) {
			text="none";
		}
		setText(text);
		
		//colors

		getElement().getStyle().setBackgroundColor("#"+model.getHex());
		
		String fgColor="000000";
		if(!model.isNone() && (model.getRed()+model.getBlue()+model.getGreen())/(255*3) < 255)
			fgColor="ffffff";
		getElement().getStyle().setColor("#"+fgColor);
		
	}
	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_COLOR}; 
	}

}